package models.merchant;

import models.BaseModel;
import models.constants.DeletedStatus;
import models.merchant.enums.TimeInterval;
import play.modules.paginate.JPAExtPaginator;

import javax.persistence.*;
import java.util.Date;
import java.util.Map;

/**
 * Created by youliangcheng on 17/7/11.
 */
@Entity
@Table(name = "power_merchant_colocations")
public class Colocation extends BaseModel {

    /**
     * 编号
     */
    @Column(name = "code")
    public String  code;

    /**
     * 标题
     */
    @Column(name = "title")
    public String  title;

    /**
     * 价格
     */
    @Column(name = "price")
    public Double  price;

    /**
     * 开始时间
     */
    @Column(name = "began_at")
    public Date beganAt;

    /**
     * 结束时间
     */
    @Column(name = "end_at")
    public Date endAt;

    /**
     * 是否生成订单
     */
    @Column(name = "create_order")
    public Boolean createOrder;

    /**
     * 标题
     */
    @Column(name = "remark")
    public String   remark;

    /**
     * 逻辑删除,0:未删除，1:已删除
     */
    @Enumerated(EnumType.ORDINAL)
    @Column(name = "deleted")
    public DeletedStatus deleted;


    /**
     * 分页查询.
     */
    public static JPAExtPaginator<Colocation> findByCondition(Map<String, Object> conditionMap, String orderByExpress, int pageNumber, int pageSize) {
        StringBuilder xsqlBuilder = new StringBuilder("t.deleted=models.constants.DeletedStatus.UN_DELETED ")
                .append("/~ and t.name like {name} ~/")
                .append("/~ and t.servicePhone = {service_phone} ~/");

        util.xsql.XsqlBuilder.XsqlFilterResult result = new util.xsql.XsqlBuilder().generateHql(xsqlBuilder.toString(), conditionMap);
        JPAExtPaginator<Colocation> orderPage = new JPAExtPaginator<Colocation>("Colocation t", "t", Colocation.class,
                result.getXsql(), conditionMap).orderBy(orderByExpress);
        orderPage.setPageNumber(pageNumber);
        orderPage.setPageSize(pageSize);
        orderPage.setBoundaryControlsEnabled(false);
        return orderPage;
    }

    public static Colocation findByColocationId(long id){
        return  Colocation.find("id = ? and deleted = ? " ,id , DeletedStatus.UN_DELETED).first();
    }



}
